package com.yanqu.road.dao.impl.activity.seacraft;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.seacraft.pirateriot.config.SeacraftPirateRiotSectionConfig;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SeacraftPirateRiotSectionConfigDaoImpl extends TempDao {

    private static String getSeacraftPirateRiotSectionConfigSql = "select * from `t_s_activity_seacraft_section` where `activityId`=?";

    public Map<Integer, Map<Integer, SeacraftPirateRiotSectionConfig>> getSeacraftPirateRiotSectionConfigMap(int activityId) {
        Map<Integer, Map<Integer, SeacraftPirateRiotSectionConfig>> mapMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getSeacraftPirateRiotSectionConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                SeacraftPirateRiotSectionConfig seacraftPirateRiotSectionConfig = new SeacraftPirateRiotSectionConfig();
                seacraftPirateRiotSectionConfig.setActivityId(rs.getInt("activityId"));
                seacraftPirateRiotSectionConfig.setChapterId(rs.getInt("chapterId"));
                seacraftPirateRiotSectionConfig.setSectionId(rs.getInt("sectionId"));
                seacraftPirateRiotSectionConfig.setMonsterId(rs.getString("monsterId"));
                seacraftPirateRiotSectionConfig.setMonsterPos(rs.getString("monsterPos"));
                seacraftPirateRiotSectionConfig.setMonsterBeHit(rs.getString("monsterBeHit"));
                seacraftPirateRiotSectionConfig.setHealth(rs.getString("health"));
                seacraftPirateRiotSectionConfig.setAttack(rs.getString("attack"));

                if (!mapMap.containsKey(seacraftPirateRiotSectionConfig.getChapterId())) {
                    mapMap.put(seacraftPirateRiotSectionConfig.getChapterId(), new ConcurrentHashMap<>());
                }
                mapMap.get(seacraftPirateRiotSectionConfig.getChapterId()).put(seacraftPirateRiotSectionConfig.getSectionId(), seacraftPirateRiotSectionConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return mapMap;
    }

}
